package com.nf28.removies;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


import android.annotation.SuppressLint;
import android.util.Base64;
import android.util.Log;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nf28.removies.api.Movie;
import com.nf28.removies.api.RequestResult;

public class Request implements ApiKey{
	
	static String  PERSONAL_API_KEY = API_KEY;
	static String API_URL = "http://api.themoviedb.org";
	static String STATIC_API_URL = "http://cf2.imgobject.com/t/p/";
	static String API_VERSION = "/3";
	
	String query = null;
	
	//equivalent of the php function http_build_query
	//transform a double map of String in http params request
	public static String http_build_query(HashMap<String, String> params) throws UnsupportedEncodingException{
		Iterator<String> i = params.keySet().iterator();
		String result = null;
		String current = i.next();
		//declare first with '?' at the beginning => ?api_key=abc&query=movie
		result = "?"+current+"="+URLEncoder.encode(params.get(current), "ISO-8859-1");
		while (i.hasNext()) {
			current = i.next();
			result += "&"+current+"="+URLEncoder.encode(params.get(current), "ISO-8859-1");
		}
		return result;
	}
	
	//transform path poster url on real url with a size
	//size "w92", "w154", "w185", "w342", "w500", "original"
	public static String path_to_url(String path, String size){		
		return STATIC_API_URL+size+path;
	}
	
	public static RequestResult searchMovie(String movieName) throws IOException{
		// method given in the API documentation http://docs.themoviedb.apiary.io/
		String method ="/search/movie";
		HashMap<String, String> map_params = new HashMap<String, String>();
		map_params.put("api_key", PERSONAL_API_KEY);
		map_params.put("query", movieName);
		map_params.put("language", "fr");
		map_params.put("search_type", "ngram");
		
		//you can test this url in your browser
		String http_url = API_URL+API_VERSION+method+http_build_query(map_params);
		Log.i("REQUEST HTTP URL :: ",http_url);		
		
		
		 DefaultHttpClient httpclient = new DefaultHttpClient();
         HttpGet httpget = new HttpGet(http_url);
         httpget.setHeader("Accept", "application/json");
         HttpResponse response = httpclient.execute(httpget);
         BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
         StringBuffer sb = new StringBuffer("");
         String line = "";
         String NL = System.getProperty("line.separator");
         while ((line = in.readLine()) != null) {                    
             sb.append(line + NL);
         }
         in.close();
         String buffer = sb.toString();
         Log.v("Json Response :: ", buffer);
         
	    ObjectMapper mapper = new ObjectMapper();
		
	    RequestResult result = null; 
	    
		 try{  
			 result = mapper.readValue(buffer, RequestResult.class);   
			}  	
		catch(Exception e1)  
		{  
		    e1.printStackTrace();  
		}
		 Log.v("page :: ", String.valueOf(result.getPage()));
		
		 return result; 
 
	}	
	
	public static RequestResult searchBoxOffice() throws IOException{
		// method given in the API documentation http://docs.themoviedb.apiary.io/
		String method ="/movie/now_playing";
		HashMap<String, String> map_params = new HashMap<String, String>();
		map_params.put("api_key", PERSONAL_API_KEY);
		
		//you can test this url in your browser
		String http_url = API_URL+API_VERSION+method+http_build_query(map_params);
		Log.i("REQUEST HTTP URL :: ",http_url);		
		
		
		 DefaultHttpClient httpclient = new DefaultHttpClient();
         HttpGet httpget = new HttpGet(http_url);
         httpget.setHeader("Accept", "application/json");
         HttpResponse response = httpclient.execute(httpget);
         BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
         StringBuffer sb = new StringBuffer("");
         String line = "";
         String NL = System.getProperty("line.separator");
         while ((line = in.readLine()) != null) {                    
             sb.append(line + NL);
         }
         in.close();
         String buffer = sb.toString();
         Log.v("Json Response :: ", buffer);
         
	    ObjectMapper mapper = new ObjectMapper();
		
	    RequestResult result = null; 
	    
		 try{  
			 result = mapper.readValue(buffer, RequestResult.class);   
			}  	
		catch(Exception e1)  
		{  
		    e1.printStackTrace();  
		}
		 Log.v("page :: ", String.valueOf(result.getPage()));
		
		 return result; 
 
	}


	public static Movie getMovieByID(int id) throws IOException{
		// method given in the API documentation http://docs.themoviedb.apiary.io/
		String method ="/movie/";
		HashMap<String, String> map_params = new HashMap<String, String>();
		map_params.put("api_key", PERSONAL_API_KEY);
		
		//you can test this url in your browser
		String http_url = API_URL+API_VERSION+method+id+http_build_query(map_params);
		Log.i("REQUEST HTTP URL :: ",http_url);		
		
		
		 DefaultHttpClient httpclient = new DefaultHttpClient();
         HttpGet httpget = new HttpGet(http_url);
         httpget.setHeader("Accept", "application/json");
         HttpResponse response = httpclient.execute(httpget);
         BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
         StringBuffer sb = new StringBuffer("");
         String line = "";
         String NL = System.getProperty("line.separator");
         while ((line = in.readLine()) != null) {                    
             sb.append(line + NL);
         }
         in.close();
         String buffer = sb.toString();
         Log.v("Json Response :: ", buffer);
         
	    ObjectMapper mapper = new ObjectMapper();
		
	    Movie result = null; 
	    
		 try{  
			 result = mapper.readValue(buffer, Movie.class);   
			}  	
		catch(Exception e1)  
		{  
		    e1.printStackTrace();  
		}
		 Log.v("resume du film :: ", String.valueOf(result.getOverview()));
		
		 return result; 
 
	}
	

}
